package com.mydemo.game.prepareStage;

import com.mydemo.entity.Car;
import com.mydemo.entity.Driver;
import com.mydemo.entity.Save;
import com.mydemo.print.Print;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PrepareStage {
    public static Save prepareStage(Save save) {
        boolean flag = true;
        while (flag) {
//            这里应该先打印一下车辆列表，以方便玩家了解当前的详情
            System.out.println("第" + save.getDay() + "天");
            System.out.println("准备阶段");
            printCarAndDriver(save);
            System.out.println("""
                    请选择接下来的操作
                    1. 为司机指定车辆
                    2. 将司机调离车辆
                    0. 进入下一个阶段
                    """);
            Scanner scanner = new Scanner(System.in);
            int choose = scanner.nextInt();
            switch (choose) {
                case 0 -> flag = false;
                case 1 -> save = DriverToCar.driverToCar(save);
                case 2 -> {
                    DriverLeftCar.driverLeftCar(save);
                }
                default -> System.out.println("输入不正确，请正确输入！");
            }
        }
        return save;
    }

    private static void printCarAndDriver(Save save) {
        List<Car> carList = save.getCarList();
        List<Driver> driverList = save.getDriverList();
        if (carList == null) {
            System.out.println("车辆列表空空如也，你也许应该租一辆车试试");
            return;
        } else {
//            把已有司机的车辆挑选出来，先打印，然后再打印没有司机的车辆
            List<Car> hasDriver = new ArrayList<>();
            List<Car> noDriver = new ArrayList<>();
            for (Car c : carList) {
                if (c.isDrive()) {
                    hasDriver.add(c);
                } else {
                    noDriver.add(c);
                }
            }
            if (hasDriver != null) {
                System.out.println("有司机的车辆：");
                for (Car car : hasDriver) {
                    Print.printCarHasDriver(car);
                }
            }
            if (noDriver != null) {
                System.out.println("无司机的车辆：");
                for (Car car : noDriver) {
                    Print.printCarNoDriver(car);
                }
            }
        }
        if (driverList == null) {
            System.out.println("司机列表空空如也，你也许应该雇一个司机试试");
            return;
        } else {
            List<Driver> driveCar = new ArrayList<>();
            List<Driver> noCar = new ArrayList<>();
            for (Driver driver :
                    driverList) {
                if (driver.getCar() != 0) {
                    driveCar.add(driver);
                } else {
                    noCar.add(driver);
                }
            }
            System.out.println("有车辆的司机");
            for (Driver driver :
                    driveCar) {
                Print.printDriver(driver);
            }
            System.out.println("无车辆的司机");
            for (Driver driver :
                    noCar) {
                Print.printDriver(driver);
            }
        }
    }
}
